package com.controller.doctor;

import com.entity.Answer;
import com.entity.patient.Needs;
import com.entity.patient.Patient;
import com.service.doctor.PatientService;
import com.util.Util;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class PatientController {
    private String path = ClassUtils.getDefaultClassLoader().getResource("static/").getPath();
    private String caseImgPath = "img/case/";
    private String excelPath = "/patient/excel/";
    @Autowired
    PatientService patientService;
    /*
        上传病例
        photoList:文件的集合
        caseInfo：数据
     */
    @PostMapping(value = "createCase")
    public Object createCase(HttpServletRequest request,List<MultipartFile> photoList,String caseInfo) {
        String doctorID =  request.getHeader("TOKEN");
        System.out.println(path);
        Patient patient = (Patient) JSONObject.toBean(JSONObject.fromObject(caseInfo),Patient.class);
        System.out.println(patient);
        patient.setDoctorID(Integer.parseInt(doctorID));

        Answer answer = (Answer)patientService.addPatiemt(patient);
        Answer a = (Answer)Util.uploadFiles(photoList,path+caseImgPath+patient.getPatientID()+"/");
        if (a == null){
            return  answer;
        }else {
            return a;
        }
    };
    /*
          查询某状态下所有的患者信息
          state:状态
     */
    @GetMapping("queryPatienstByState")
    public Object queryPatienstByState(String state,HttpServletRequest request){
        String doctorID =  request.getHeader("TOKEN");
        Answer answer =(Answer)patientService.queryCaseState(state,Integer.parseInt(doctorID));
        System.out.println(answer.getData()+state);
        return answer;
    }

    /*
           删除病例的所有信息
           patientID:患者ID;
     */
    @GetMapping("deleteCaseAllInformation")
    public Object deleteCaseAllInformation( int patientID,HttpServletRequest request){
        String doctorID =  request.getHeader("TOKEN");
        return patientService.deleteCaseAllInformation(patientID,Integer.parseInt(doctorID));
    }

    /**
     * patientID:患者ID
      * title:标题
     * supplementary：补充说明
     * state:病列状态
     */
    @GetMapping("addNeeds")
    public Object addNeeds(Needs needs){
        return patientService.addNeeds(needs);
    }

    /**
     * 医生找专家
     * data ：Needs类的json数据
     * caseID：病例ID
     * expertID:专家ID
     */
    @GetMapping("seekExperts")
    public Object  seekExperts(String data,int caseID,int expertID,HttpServletRequest request){
        String doctorID =  request.getHeader("TOKEN");
        Needs needs = (Needs)JSONObject.toBean(JSONObject.fromObject(data),Needs.class);
        return patientService.seekExperts(caseID,expertID,Integer.parseInt(doctorID),needs);
    }

    /**
     * 用户详情查询
     * @param patientID:患者编号
     * @return
     */
    @GetMapping("public/queryPatientDetails")
    public Object publicQueryPatientDetails(Integer patientID){
        return patientService.queryPatientDetails(patientID);
    }

    @GetMapping("queryPatientDetails")
    public Object queryPatientDetails(Integer patientID){
        return patientService.queryPatientDetails(patientID);
    }

}

